﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;

namespace HSIA.SMS
{
    public class WebSms
    {
        private static string _url = "http://121.199.48.186:1210/Services/MsgSend.asmx";
        private static string _userid, _password;

        /// <summary>
        ///  获取账户余额
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static int GetBalance(string userid = "llxt", string password = "llxt5858")
        {
            _userid = userid;
            _password = password;
            string[] paramName = new string[2];
            string[] paramValue = new string[2];
            paramName[0] = "userCode";
            paramName[1] = "userPass";
            paramValue[0] = _userid;
            paramValue[1] = _password;
            string res = PostRequest(_url + "/GetBalance", paramName, paramValue);
            return PublicMethod.GetInt(res);
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="DesNo"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public static int SendMsg(string DesNo, string Msg)
        {
            string[] paramName = new string[5];
            string[] paramValue = new string[5];
            paramName[0] = "userCode";
            paramName[1] = "userPass";
            paramName[2] = "DesNo";
            paramName[3] = "Msg";
            paramName[4] = "Channel";
            paramValue[0] = _userid;
            paramValue[1] = _password;
            paramValue[2] = DesNo;
            paramValue[3] = Msg;
            paramValue[4] = "0";
            string res = PostRequest(_url + "/SendMsg", paramName, paramValue);
            return PublicMethod.GetInt(res);
        }

        /// <summary>
        /// 获取发送状态
        /// </summary>
        /// <param name="batchNumber"></param>
        /// <returns></returns>
        private static int GetReport(string batchNumber)
        {
            string[] paramName = new string[3];
            string[] paramValue = new string[3];
            paramName[0] = "userCode";
            paramName[1] = "userPass";
            paramName[2] = "batchNumber";
            paramValue[0] = _userid;
            paramValue[1] = _password;
            paramValue[2] = batchNumber;
            string res = PostRequest(_url + "/GetReport", paramName, paramValue);
            return 0;
        }

        /// <summary>
        /// 读取全部短信
        /// </summary>
        /// <returns></returns>
        public static string GetMo()
        {
            string[] paramName = new string[2];
            string[] paramValue = new string[2];
            paramName[0] = "userCode";
            paramName[1] = "userPass";
            paramValue[0] = _userid;
            paramValue[1] = _password;
            string res = PostRequest(_url + "/GetReport", paramName, paramValue);
            return res;
        }

        /// <summary>
        /// 读取指定批号短信
        /// </summary>
        /// <param name="minId"></param>
        /// <returns></returns>
        public static string GetMoExt(string minId)
        {
            string[] paramName = new string[2];
            string[] paramValue = new string[2];
            paramName[0] = "userCode";
            paramName[1] = "userPass";
            paramName[2] = "minId";
            paramValue[0] = _userid;
            paramValue[1] = _password;
            paramValue[2] = minId;
            string res = PostRequest(_url + "/GetReport", paramName, paramValue);
            return res;
        }

        private static string PostRequest(string url, string[] paramName, string[] paramValue)
        {
            StringBuilder sbuilder = new StringBuilder(paramName[0] + "=" + paramValue[0]);
            for (int i = 1; i < paramName.Length; i++)
            {
                sbuilder.Append("&" + paramName[i] + "=" + paramValue[i]);
            }
            new ASCIIEncoding().GetBytes(sbuilder.ToString());

            #region get方式
            //string URL = url + "?" + sbuilder.ToString();
            //try
            //{
            //    Uri uri = new Uri(URL);
            //    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            //    req.Timeout = 15000;
            //    HttpWebResponse wresp = (HttpWebResponse)req.GetResponse();
            //    StreamReader objReader = new StreamReader(wresp.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            //    XmlDocument xml = new XmlDocument();//初始化一个xml实例
            //    xml.Load(objReader);
            //    objReader.Close();

            //    return xml.DocumentElement.InnerText;
            //}
            //catch (Exception ex)
            //{
            //    return ex.Message;
            //}
            #endregion

            #region post方式
            Uri uri = new Uri(url);
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.Timeout = 10000;
                byte[] data = Encoding.UTF8.GetBytes(sbuilder.ToString());//参数
                req.ContentLength = data.Length;
                Stream writer = req.GetRequestStream();
                writer.Write(data, 0, data.Length);
                writer.Close();

                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                String retXml = sr.ReadToEnd();
                sr.Close();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retXml);
                sr.Close();

                return doc.DocumentElement.InnerText;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            #endregion
        }

    }
}
